package com.open4cn.simulator.config.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.open4cn.simulator.config.entity.DeviceEntity;
import com.open4cn.simulator.config.server.IDeviceServer;
import com.open4cn.simulator.core.exception.IllegalParameterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-04 04:02
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private IDeviceServer deviceServer;

    @PostMapping("/add")
    public Object add(@RequestBody DeviceEntity device) {
        if (CharSequenceUtil.isBlank(device.getBrand()) || CharSequenceUtil.isBlank(device.getModel())) {
            throw new IllegalParameterException();
        }
        return deviceServer.add(device);
    }

    @PostMapping("/update")
    public Object update(@RequestBody DeviceEntity device) {
        if (CharSequenceUtil.isBlank(device.getId())) {
            throw new IllegalParameterException();
        }
        return deviceServer.update(device);
    }

    @GetMapping("/{id}")
    public Object get(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        return deviceServer.get(id);
    }

    @GetMapping("/list")
    public Object list() {
        return deviceServer.list();
    }

}
